package com.rrx.mapper;

import com.rrx.model.Users;
import org.apache.ibatis.annotations.*;
import java.sql.Timestamp;

@Mapper
/**
 * 数据库操作
 */
public interface UsersMapper {
    /**
     * 通过openID查询数据库是否存在此openID
     * @param openid
     * @return
     */
    @Select("select openid from users where openid=#{openid}")
    public String getOpenid(@Param("openid") String openid);

    /**
     * 添加用户openID和第一次登陆时间
     * @param openid
     * @param time
     * @return
     */
    @Insert("insert into users (openid,time) values(#{openid},#{time})")
    public int setOpenid(@Param("openid") String openid,@Param("time") Timestamp time);



    /**
     * 通过openID查询得到用户状态是否被冻结
     * @param openid
     * @return
     */
    @Select("select state from users where openid=#{openid}")
    public Integer selectState(@Param("openid") String openid);

    /**
     * 通过openID查询得到用户ID
     * @param openid
     * @return
     */
    @Select("select id from users where openid=#{openid}")
    public Integer selectUserid(@Param("openid") String openid);
    @Select("select openid from users where id=#{id}")
    public String selectOpenId(@Param("id") int id);

    /**
     * 查出电话号码
     * @param openid
     * @return
     */
    @Select("select phone from users where openid=#{openid}")
    public String selectPhone(@Param("openid") String openid);

    /**
     * 查询（userid,phone,address)
     * @return
     */
    @Select("select u.id as id,u.phone as phone,concat(a.province,a.city,a.area,a.location) as addr, u.state as state from users u left join address a on u.address = a.id where u.openid = #{openid} and u.state = 0")
     @Results({
             @Result(id=true,column = "id",property = "id"),
             @Result(column = "phone",property = "phone"),
             @Result(column = "addr",property = "address"),
             @Result(column = "state", property = "state")
     })
    public Users getUserInfo(@Param("openid") String openid);

}